package com.tbm.console.proj.service.impl;

import com.tbm.console.base.service.CommonServiceImpl;
import com.tbm.console.proj.entity.ColumnEntity;
import com.tbm.console.proj.repository.ColumnRepository;
import com.tbm.console.proj.repository.EquipmentRepository;
import com.tbm.console.proj.service.ColumnService;
import com.tbm.console.proj.service.EquipmentService;
import com.tbm.console.util.PageData;
import com.tbm.console.util.StringsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ColumnServiceImpl extends CommonServiceImpl<ColumnEntity> implements ColumnService{

    @Autowired
    private ColumnRepository columnRepository;



    @Override
    public List<ColumnEntity> findAll(PageData pageData, Integer start, Integer length) {
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        //使用 CriteriaBuilder方式来做过滤
        Specification<ColumnEntity> sp = this.getSpecification(pageData);
        List<ColumnEntity> all = columnRepository.findAll(sp,pageable).getContent();
        return all;
    }

    @Override
    public long count(PageData pageData) {
        return columnRepository.count(this.getSpecification(pageData));
    }

    @Override
    public List<ColumnEntity> saveAll(List<ColumnEntity> columnEntities) {
        return columnRepository.saveAll(columnEntities);
    }

    @Override
    public List<ColumnEntity> findAllById(Integer[] ids) {
        List<ColumnEntity> list = columnRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            ColumnEntity equipmentEntity = new ColumnEntity();
            list.add(equipmentEntity);
        }
        return list;
    }

    @Override
    public Specification<ColumnEntity> getSpecification(PageData pageData) {
        return new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null != pageData.get("tableName")) {
                    preList.add(criteriaBuilder.equal(root.get("tableName"), pageData.get("tableName").toString()));
                }
                if (null != pageData.get("freezeName")) {
                    preList.add(criteriaBuilder.equal(root.get("freezeName"), pageData.get("freezeName").toString()));
                }
                if (null != pageData.get("displayName")) {
                    preList.add(criteriaBuilder.equal(root.get("displayName"), pageData.get("displayName").toString()));
                }
                if (null != pageData.get("hiddenName")) {
                    preList.add(criteriaBuilder.equal(root.get("hiddenName"), pageData.get("hiddenName").toString()));
                }
                if (null != pageData.get("allName")) {
                    preList.add(criteriaBuilder.equal(root.get("allName"), pageData.get("allName").toString()));
                }
                if (null != pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                if (null != pageData.get("className")) {
                    preList.add(criteriaBuilder.equal(root.get("className"), pageData.get("className").toString()));
                }

                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }

    @Override
    public List<String> findAllTables() {
        List<String> list = columnRepository.findAllTables();
        List<String> resultList = new ArrayList<>();
        for(String str : list){
            resultList.add(StringsUtil.upperCase(str.replace("ta_",""))+"Entity");
        }
        return resultList;
    }
}
